package com.heima.media.controller;

import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.entity.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.service.IWmChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
@Api(tags = "频道管理API")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {
    @Autowired
    IWmChannelService service;
    @ApiOperation(value = "根据名称模糊查询分页列表", notes = "author:mcm")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "查询对象", required = true, dataType = "ChannelDto")
    @PostMapping("/list")
    public ResponseResult listByName(@RequestBody ChannelDto dto){
        return service.listByName(dto);
    }
    @PostMapping
    public ResponseResult add(@RequestBody WmChannel wmChannel){
            if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
            }
            WmChannel wmChannel1 = service.getById(wmChannel.getId());
            if (wmChannel1 != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            wmChannel.setCreatedTime(new Date());
        service.save(wmChannel);
        return ResponseResult.okResult();
    }
}
